package cn.com.w.ratelimiter.controller;


import cn.com.w.ratelimiter.annotation.Limiting;
import cn.com.w.ratelimiter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private HelloService helloService;


    @GetMapping("/class/{userName}")
    @Limiting(limitNum = 2, name = "hello")
    public String hello(@PathVariable("userName") String userName){
        return "hello " + userName;
    }

    @GetMapping("/method/{userName}")
    public String hello1(@PathVariable("userName") String userName){
        helloService.helloFor(userName);

        /*
         * 不可进行下面这样调用，spring 为当前类生成了代理类，然后执行相关方法时，判断有没有@Limiting这个注解，，如果有的话，则会进行拦截。但是下面的调用方式使用的并不是代理对象，从而不回进行拦截。
         */
        //this.helloForLimit(userName);
        return "hello " + userName;
    }


    @Limiting(limitNum = 2, name = "helloForLimit")
    public void helloForLimit(String userName){
        System.out.println("hello " + userName);
    }

}
